//
//  AddDecryptVC.m
//  MyProject
//
//  Created by jiaming yan on 2018/3/22.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "AddDecryptVC.h"
#import "AddDecryptTitleVC.h"
#import "CurrentStatusPopView.h"
#import "AddDecryptDetailVC.h"
#import "NSUDManager.h"
@interface AddDecryptVC ()

@property (weak, nonatomic) IBOutlet UILabel *typeNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UITextField *priceTextField;
@property (weak, nonatomic) IBOutlet UILabel *contentLabel;
@property (weak, nonatomic) IBOutlet UIButton *openButton;
@property (copy, nonatomic) NSString *typeStr;
@property (weak, nonatomic) IBOutlet UIButton *bottonView;
@property (strong, nonatomic) NSArray *typeArr;
@property (strong, nonatomic) NSArray *typeStatueArr;
@end

@implementation AddDecryptVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.typeArr = @[@"公司解密", @"求职能力", @"专业技能", @"职业规划", @"职场生存", @"创业辅导", @"其他"];
    self.typeStatueArr = @[@"DECRYPT_GSJM", @"DECRYPT_QZNL", @"DECRYPT_ZYJN", @"DECRYPT_ZYGH", @"DECRYPT_ZCSC", @"DECRYPT_CYFD", @"DECRYPT_QT"];
    self.navigationItem.title = @"解密专题";
    self.typeStr = @"DECRYPT_GSJM";
    [self.openButton setImage:IMAGE(@"switch_colos") forState:UIControlStateNormal];
    [self.openButton setImage:IMAGE(@"switch_open") forState:UIControlStateSelected];
    [self loadRightButton];
    if (self.model) {
        self.typeStr = self.model.type;
        self.titleLabel.text = self.model.title;
        for (NSInteger i = 0; i < self.typeStatueArr.count; i++) {
            if ([self.typeStatueArr[i] isEqualToString:self.typeStr]) {
                self.typeNameLabel.text = self.typeArr[i];
                break;
            }
        }
        self.contentLabel.text = self.model.details;
        self.openButton.selected = self.model.isEnable;
        self.priceTextField.text = [NSString stringWithFormat:@"%.2f",self.model.price];
        self.bottonView.hidden = NO;
    }
}
-(void)loadRightButton{

    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 40, 30);
    [backBtn setTitle:@"完成" forState:UIControlStateNormal];
    backBtn.titleLabel.font = k14Font;
    [backBtn setTitleColor:UIColorFromRGB(0X474C4D) forState:UIControlStateNormal];
    //    [backBtn setBackgroundImage:[UIImage imageNamed:@"公司icon"] forState:UIControlStateNormal];
        [backBtn addTarget:self action:@selector(finishClick) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    UIBarButtonItem *negativeSpacer = [[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
                                       target:nil action:nil];
    negativeSpacer.width = -20;
    self.navigationItem.rightBarButtonItems = @[negativeSpacer,backItem];
    if (IOS11Later) {
        backBtn.contentHorizontalAlignment =UIControlContentHorizontalAlignmentRight;
    }
}
-(void)finishClick{
    NSString *msg = @"";
    if (self.typeStr.length == 0) {
        msg = @"请选择解密类型";
    }else if(self.titleLabel.text.length == 0){
        msg = @"请输入解密标题";
    }else if(self.priceTextField.text.length == 0){
        msg = @"请输入价格";
    }else if(self.contentLabel.text.length == 0){
        msg = @"请输入解密内容";
    }
    if (msg.length > 0) {
        ALERT_VIEW(msg);
        return;
    }
    [self showProgressWithView:self.view animated:YES];
    NSDictionary *dic = @{@"type":self.typeStr,@"price":self.priceTextField.text,@"isEnable":[NSNumber numberWithBool:self.openButton.selected],@"title":self.titleLabel.text,@"details":self.contentLabel.text};
    if (self.model) {
        
        dic = @{@"incumbentDecryptId":[NSNumber numberWithInteger:self.model.incumbentDecryptId],@"type":self.typeStr,@"price":self.priceTextField.text,@"isEnable":[NSNumber numberWithBool:self.openButton.selected],@"title":self.titleLabel.text,@"details":self.contentLabel.text};
    }
    [YJMSendRequest postDataWithParameters:dic url:@"decrypt/updateIncumbentDecrypt" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (IBAction)buttonClick:(UIButton *)sender {

    __weak typeof(self) weakSelf = self;
    switch (sender.tag) {
        case 1:
        {
            /**类型*/
 
            CurrentStatusPopView *statusV = [[CurrentStatusPopView alloc] initWithStatusArray:self.typeArr andSelectedStatus:self.typeNameLabel.text];
            statusV.titleStr = @"选择类型";
            [statusV setSelectBlock:^(NSString *status) {
                weakSelf.typeNameLabel.text = status;
                for (NSInteger i = 0; i < self.typeArr.count; i++) {
                    if ([self.typeArr[i] isEqualToString:status]) {
                        self.typeStr = self.typeStatueArr[i];
                        break;
                    }
                }
            }];
            [statusV show];
        }
            break;
        case 2:
        {
            /**标题*/
            AddDecryptTitleVC *vc = [[AddDecryptTitleVC alloc]init];
            vc.titleStr = self.titleLabel.text;
            vc.titleSelect= ^(NSString *titleStr){
                weakSelf.titleLabel.text = titleStr;
            };
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 3:
        {
            /**内容*/
            AddDecryptDetailVC *vc = [[AddDecryptDetailVC alloc]init];
            vc.contentStr = self.contentLabel.text;
            vc.typeStr = @"1";
            vc.contentSelect = ^(NSString *contentStr) {
                weakSelf.contentLabel.text = contentStr;
            };
            [self.navigationController pushViewController:vc animated:YES];
            
        }
            break;
        case 4:
        {
            /**开启关闭*/
            sender.selected = !sender.selected;
        }
            break;
        default:
            break;
    }
}
- (IBAction)deleteButtonClick:(id)sender {
    [self showProgressWithView:self.view animated:YES];
    [YJMSendRequest postDataWithParameters:@{@"incumbentDecryptId":[NSNumber numberWithInteger:self.model.incumbentDecryptId]} url:@"decrypt/deleteIncumbentDecrypt" result:^(NSDictionary *result, NSError *error) {
        result = [CommonUtils decryptDicWithResult:result];
        [self hideProgress:self.view animated:YES];
        if([result[@"result"] integerValue] == 1)
        {
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [CommonUtils showToast:[result objectForKey:@"msg"]];
        }
    }];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
